how 2 GET last non-empty row number

abyss

Board Regular
Joined
Mar 24, 2004
Messages
68
i need a number, NOT a region.
a simple integer.
how can i get it? (VBA)
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Code:
Sub lastrow()
Dim lastrow As Long

lastrow = Range("A65536").End(xlUp).Row
MsgBox "" & lastrow

End Sub

You could use xlDown if your data contains no gaps

Hope this helps,

Edit: BTW - use Long as opposed to Integer, otherwise you will encounter problems if your lastrow > 32,767
 
Upvote 0
May I add something in case not all columns contain data until the same row
Code:
Sub lastrow()
Dim lastrow As Long
Dim col As Integer
For col = 1 To Columns.Count
lastrow = Application.WorksheetFunction.Max(Cells(65536, col).End(xlUp).Row, lastrow)
Next col
MsgBox "Last non-empty rownumber :" & lastrow
End Sub

regards,
Erik
 
Upvote 0
You're welcome
thanks for compliment
this code just checks all 256 columns and stores the highest value

still wondering if there would be a quicker way, but it would need perhaps more code
I can't advise
Code:
ActiveSheet.UsedRange.Rows.Count
since this code sometimes finds empty cells as being used

kind regards,
Erik
 
Upvote 0
use this function - I got it from here I think
Eric, your method doesn't work I'm afraid (eg put something into IV65536 and try it)
Code:
Function LastCell(ws As Worksheet) As Range
  Dim LastRow, LastCol
' Error-handling is here in case there is not any
' data in the worksheet
  On Error Resume Next
  With ws
  ' Find the last real row
    LastRow = .Cells.Find(What:="*", _
      SearchDirection:=xlPrevious, _
      SearchOrder:=xlByRows).Row
  ' Find the last real column
    LastCol = .Cells.Find(What:="*", _
      SearchDirection:=xlPrevious, _
      SearchOrder:=xlByColumns).column
  End With
' Finally, initialize a Range object variable for
' the last populated row.
  Set LastCell = ws.Cells(LastRow, LastCol)
  Cells(LastRow, LastCol).Select

 If IsEmpty(LastRow) Then
  MsgBox ("No data in worksheet")
  End
  End If

End Function

'''''''''
Sub RealLastCell()

   rlc = LastCell(ActiveSheet).Address(False, False)
   lr = ActiveCell.Row
   lc = ActiveCell.column

End Sub
 
Upvote 0
abyys,
but we always try to find solutions that work anytime AND as quick as possible
therefore my statement "still wondering if there would be a quicker way, but it would need perhaps more code"
good job, pcc

regards,
Erik
 
Upvote 0
Cheers. I wasn't being critical Eric - hope no offence was taken. We are all learning this stuff!
 
Upvote 0

Forum statistics

Threads
1,215,378
Messages
6,124,603
Members
449,174
Latest member
ExcelfromGermany

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top